package com.solming.leetcode.question;

import java.util.HashMap;

public class LengthOfLongestSubstring3 {


    public static int lengthOfLongestSubstring(String s) {
        int len = s.length();

        int right = 0;
        int max = 0;
        if (len == 0) {
            return 0;
        }
        char[] c = s.toCharArray();
        HashMap<Character, Integer> characterIntegerHashMap = new HashMap<>();
        int left = 0;
        //abcabcbb - > abc
        while (right < len) {
            char c1 = c[right];
            if (characterIntegerHashMap.containsKey(c1)) {
                int index = characterIntegerHashMap.get(c1);
                left = Math.max(index,left);
                max = Math.max(right - left+1, max);

            } else {
                max = Math.max(right - left+1 , max);
            }
            characterIntegerHashMap.put(c1, right+1);
            right++;
        }
        return max;


    }


    public static int lengthOfLongestSubstring1(String s) {
        HashMap<Character,Integer> charIndexMap =new HashMap<>();
        char[] chars = s.toCharArray();
        int len = chars.length;
        int max = 0;
        for (int start = 0 , end =0 ; end <len ; end ++){
            Character c =  chars[end];
            if (charIndexMap.containsKey(c)){
                start = Math.max(charIndexMap.get(c),start);
            }
            charIndexMap.put(c,end+1);
            max = Math.max(max,end-start+1);
        }
        return max;
    }


    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("bbbbb"));
    }
}
